﻿namespace Blaze.UI
{
    using UnityEngine;
    using UnityEngine.Assertions;

    /// <summary>
    /// 用于加载JPG/PNG图片。
    /// </summary>
    public class EncodedTexture : MonoBehaviour
    {
        public TextAsset Asset;
        public Material Material;
        public Vector2 Size;

        private void Start()
        {
            var width = (int) Size.x;
            var height = (int) Size.y;

            Assert.IsTrue(width > 0);
            Assert.IsTrue(height > 0);

            var texture = new Texture2D(width, height, TextureFormat.RGB24, false);
            texture.LoadImage(Asset.bytes, true);
            Material.mainTexture = texture;
            Asset = null;
        }
    }
}